from sqlalchemy import Column, String, Text, Integer, DateTime, ForeignKey
from sqlalchemy.sql import func
from app.models.database import Base

class Category(Base):
    """分类模型"""
    __tablename__ = "categories"

    id = Column(String(36), primary_key=True, index=True)
    name = Column(String(100), nullable=False, index=True)
    description = Column(Text)
    
    # 图标和颜色
    icon = Column(String(50))  # 图标名称
    color = Column(String(20))  # 颜色代码
    
    # 父分类（支持树形结构）
    parent_id = Column(String(36), ForeignKey('categories.id', ondelete='CASCADE'), nullable=True)
    
    # 排序
    sort_order = Column(Integer, default=0)
    
    # 创建信息
    created_by = Column(String(36), ForeignKey('users.id', ondelete='SET NULL'))
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    def __repr__(self):
        return f"<Category(name='{self.name}')>"
